const express = require(`express`);
const db_query = require('../../utils/db_query');
const db = require('../../utils/db_connect');
const router = express.Router();
const nodemailer = require("nodemailer");
const smtpTransport = require('nodemailer-smtp-transport');
const xss = require('xss');

let myxss = new xss.FilterXSS({
    whiteList: {
        a: ["href", "title", "target"],
    },
});
// 以后直接调用 myxss.process() 来处理即可
// html = myxss.process('<script>alert("xss");</script>');



//获取授权列表
router.get('/ipAuth/list',(req,res)=>{
    let {status} = req.query;
    let sql = 'select * from ip_auth';
    let sqlArr = []
    if(status){
        sql = `select * from ip_auth  WHERE status=?`;
        sqlArr = [status]
    }

    db_query(sql,sqlArr,res,(err,data)=>{
        if(err) return
        res.send({
            code:'200',
            message:'查询成功',
            data:data
        })
    })

})

//授权申请
router.post('/ipAuth/apply',(req,res)=>{
    let {dept_id,wx_name,project_name,range_status,description,email,type} = req.body;
    wx_name = myxss.process(wx_name);
    project_name = myxss.process(project_name);
    description = myxss.process(description);
    let sql = `INSERT INTO ip_auth(dept_id,wx_name,project_name,range_status,description,status,email,type,create_time) values(?,?,?,?,?,?,?,?,now())`;
    let sqlArr = [dept_id,wx_name,project_name,range_status.toString(),description,1,email,type]
    db_query(sql,sqlArr,res,(err,data)=>{
        if(err) return
        res.send({
            code:'200',
            message:'申请成功',
            data:null
        })
    })
})


//审批申请
router.post('/ipAuth/examine',(req,res)=>{
    let {status,cause,design_img,id,email} = req.body;
    cause = myxss.process(cause);
    let roleArr = ['1','2']
    if(!roleArr.includes(req.headers.role)){
        res.send({
            code: '401',
            message: '你没有权限访问',
            data: null
        })
        return;
    }

    let sql = `UPDATE ip_auth SET status=?,cause=?,design_img=? WHERE id=?`;
    let sqlArr = [status,cause,design_img,id]
    db_query(sql,sqlArr,res,(err,data)=>{
        if(err) return;

        if(status == 5){    //发送邮件
            emailTo('14718387650@163.com','设计规范','123','',function(data){
                res.send({
                    code:'200',
                    message:'设计素材已发送至邮箱',
                    data:null
                })
            })
            return
        }

        res.send({
            code:'200',
            message:'提交成功',
            data:null
        })
    })
})


function emailTo(email,subject,text,html,callback){

    var transporter  = nodemailer.createTransport(smtpTransport({
        host:'smtp.qq.com',
        secure: true,
        secureConnection: true, // 使用 SSL
        port: 465, // SMTP 端口
        auth:{
            user:'1215624410@qq.com',
            pass:'dsawxfdguyzwhijc'
        }
    }))
    var mailOptions = {
        from:'1215624410@qq.com',  //发送者
        to:'396004659@qq.com', //接收者
        subject:subject ,//标题
        text:text,
        html:html,
        attachments: [
            {
                filename: 'abc.zip',
                path: __dirname + '/abc.zip'
            }
        ]
    }
    
    var result = {
        code:200,
        message:'发送成功'
    }

    try{
        transporter.sendMail(mailOptions,function(err,info){
            if(err){
                result.code = 500;
                result.message = err;
                callback(result);
                return;
            }
            callback(result)
        })
    }catch (err){
        result.code = 500;
        result.message = err;
        callback(result)
    }

    transporter.close()
}

module.exports = router